﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.IoC;
using Kooboo.Domain;

namespace Kooboo.Commerce.Contexts.Promotions
{
    public static class EditorBehavior
    {
        #region PromotionRule
        public static void Delete(this IEditor actor, IPromotionRule rule)
        {
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    var repository = ContextContainer.Current.Resolve<IPromotionRuleRepository>();
                    repository.Delete(rule);

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }

        public static void Post(this IEditor actor, IPromotionRule promotionRule)
        {

            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    promotionRule.RemoveSeftReference();
                    var repository = ContextContainer.Current.Resolve<IPromotionRuleRepository>();
                    repository.Post(promotionRule);

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

        }

        public static void Put(this IEditor actor, IPromotionRule promotionRule)
        {

            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    promotionRule.RemoveSeftReference();
                    var repository = ContextContainer.Current.Resolve<IPromotionRuleRepository>();
                    repository.Put(promotionRule);

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

        } 
        #endregion

       
    }
}
